package arrAndstr;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/*
    205. 同构字符串
    大佬做法：
        因为indexOf(xx)是获取xx的第一个坐标，等于校验了是否唯一替代
        for(int i = 0; i < s.length(); i++){
          if(s.indexOf(s.charAt(i)) != t.indexOf(t.charAt(i))){
              return false;
          }
        }
        return true;
 */
public class T205 {

    public static void main(String[] args) {
        String s = "bbbaaaba";
        String t = "aaabbbba";
        System.out.println(isIsomorphic(s, t));
    }

    public static boolean isIsomorphic(String s, String t) {
        Map<Character, Character> map = new HashMap<>();
        char[] chars1 = s.toCharArray();
        char[] chars2 = t.toCharArray();

        for (int i = 0; i < chars1.length; i++) {   // 把对应关系记下来
            char ch1 = chars1[i];
            char ch2 = chars2[i];

            if (!map.containsKey(ch1) && map.containsValue(ch2)) {
                return false;
            }

            if (!map.containsKey(ch1)) {
                map.put(ch1, ch2);
            }
        }

        // 开始转化s
        for (int i = 0; i < chars1.length; i++) {
            chars1[i] = map.get(chars1[i]);
        }
        s = String.copyValueOf(chars1);

        System.out.println(s);
        System.out.println(t);

        return s.equals(t);
    }
}
